/**
 * createby : JinxLbj
 * date : 2018/8/8
 * desc : 项目控制器
 **/

package com.example.demo.biz.project.controller;

import com.example.demo.biz.project.service.ProjectService;
import com.example.demo.common.entity.Result;
import com.example.demo.common.enums.ResultEnum;
import com.example.demo.netty.NettyServer;
import com.example.demo.netty.service.NettyService;
import com.jcraft.jsch.ChannelExec;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping(value = "project")
public class ProjectController {

    @Autowired
    private ProjectService projectServiceImpl;

    @ApiOperation(value = "Clone项目", notes = "通过url来进行项目克隆")
    @ApiImplicitParams({@ApiImplicitParam(name = "projectName", value = "项目名", required = true, dataType = "varchar"),
            @ApiImplicitParam(name = "url", value = "地址", required = true, dataType = "varchar"),
            @ApiImplicitParam(name = "username", value = "用户名（私人项目）", required = false, dataType = "varchar"),
            @ApiImplicitParam(name = "password", value = "密码（私人项目）", required = false, dataType = "varchar")})
    @PostMapping(value = "clone")
    public Result clone(String projectName, String url, String username, String password) {
        if (projectName == null || url == null || "".equals(projectName) || "".equals(url)) {
            return Result.buildResult(ResultEnum.PARAM_NULL, null);
        }
        return projectServiceImpl.cloneProject(projectName, url, username, password);
    }

    @ApiOperation(value = "pull项目", notes = "从Git更新项目")
    @ApiImplicitParams({@ApiImplicitParam(name = "projectName", value = "项目名", required = true, dataType = "varchar"),
            @ApiImplicitParam(name = "remoteName", value = "仓库地址默认为当前地址", required = false, dataType = "varchar"),
            @ApiImplicitParam(name = "branchName", value = "分支名（默认为master）", required = false, dataType = "varchar"),
            @ApiImplicitParam(name = "username", value = "用户名（私人项目）", required = false, dataType = "varchar"),
            @ApiImplicitParam(name = "password", value = "密码（私人项目）", required = false, dataType = "varchar")})
    @PostMapping(value = "pull")
    public Result pull(String projectName, String remoteName, String branchName, String username, String password) {
        if (projectName == null || username == null || password == null || "".equals(projectName)) {
            return Result.buildResult(ResultEnum.PARAM_NULL, null);
        }
        return projectServiceImpl.pullProject(projectName, branchName, remoteName, username, password);
    }

    @ApiOperation(value = "打包后台项目", notes = "打包后台项目")
    @ApiImplicitParams({@ApiImplicitParam(name = "projectName", value = "项目名", required = true, dataType = "varchar"),
            @ApiImplicitParam(name = "type", value = "方式（无用）", required = false, dataType = "varchar"),
            @ApiImplicitParam(name = "config", value = "自定义配置", required = false, dataType = "varchar"),
            @ApiImplicitParam(name = "packageName", value = "打包后文件名", required = true, dataType = "varchar")})
    @PostMapping(value = "runBack")
    public Result runBack(String projectName, Integer type, String config, String packageName) {
        if (projectName == null || "".equals(projectName)) {
            return Result.buildResult(ResultEnum.PARAM_NULL, null);
        }
        return projectServiceImpl.runProjectBack(projectName, type, config, packageName);
    }

    @ApiOperation(value = "构建前台项目", notes = "构建前台项目")
    @ApiImplicitParams({@ApiImplicitParam(name = "projectName", value = "项目名", required = true, dataType = "varchar"),
            @ApiImplicitParam(name = "type", value = "方式（0 : npm 1 : yarn）", required = true, dataType = "varchar")})
    @PostMapping(value = "buildFront")
    public Result buildFront(String projectName, Integer type) {
        if (projectName == null || "".equals(projectName)) {
            return Result.buildResult(ResultEnum.PARAM_NULL, null);
        }
        return projectServiceImpl.runProjectFront(projectName, type);
    }

    @ApiOperation(value = "停止项目（弃用）", notes = "停止项目（弃用）")
    @ApiImplicitParams({@ApiImplicitParam(name = "projectName", value = "项目名", required = true, dataType = "varchar")})
    @PostMapping(value = "stop")
    public Result stop(String projectName) {
        if (projectName == null || "".equals(projectName)) {
            return Result.buildResult(ResultEnum.PARAM_NULL, null);
        }
        return projectServiceImpl.stopProject(projectName);
    }

    @ApiOperation(value = "发布前台项目", notes = "将前台项目发布到指定服务器")
    @ApiImplicitParams({@ApiImplicitParam(name = "projectName", value = "项目名", required = true, dataType = "varchar"),
            @ApiImplicitParam(name = "host", value = "服务器IP", required = true, dataType = "varchar"),
            @ApiImplicitParam(name = "port", value = "服务器端口（默认22）", required = false, dataType = "varchar"),
            @ApiImplicitParam(name = "ftpUsername", value = "前端服务器权限用户名", required = true, dataType = "varchar"),
            @ApiImplicitParam(name = "ftpPassword", value = "密码", required = true, dataType = "varchar"),
            @ApiImplicitParam(name = "dst", value = "发布位置", required = true, dataType = "varchar")})
    @PostMapping(value = "upload")
    public Result upload(String host, Integer port, String ftpUsername, String ftpPassword, String dst, String projectName) {
        return projectServiceImpl.uploadProject(host, port, ftpUsername, ftpPassword, dst, projectName);
    }

    @ApiOperation(value = "发布后台项目", notes = "将后台项目发布到指定服务器")
    @ApiImplicitParams({@ApiImplicitParam(name = "projectName", value = "项目名", required = true, dataType = "varchar"),
            @ApiImplicitParam(name = "host", value = "服务器IP", required = true, dataType = "varchar"),
            @ApiImplicitParam(name = "port", value = "服务器端口（默认22）", required = false, dataType = "varchar"),
            @ApiImplicitParam(name = "ftpUsername", value = "后端服务器权限用户名", required = true, dataType = "varchar"),
            @ApiImplicitParam(name = "ftpPassword", value = "密码", required = true, dataType = "varchar"),
            @ApiImplicitParam(name = "dst", value = "发布位置", required = true, dataType = "varchar")})
    @PostMapping(value = "uploadBackEnd")
    public Result uploadBackEnd(String host, Integer port, String ftpUsername, String ftpPassword, String dst, String projectName) {
        return projectServiceImpl.uploadProjectBackEnd(host, port, ftpUsername, ftpPassword, dst, projectName);
    }

    @ApiOperation(value = "获取当前运行的项目（弃用）", notes = "获取当前运行的项目（弃用）")
    @GetMapping(value = "getProcess")
    public Result getProcess() {
        return projectServiceImpl.getRunningProject();
    }
}
